
#include <stdio.h>
#include <string.h>
#include "xdisk.h"
#include "xfat.h"

extern xdisk_driver_t vdisk_driver;

const char *disk_path_test = "disk_test.img";
const char *disk_path = "disk.img";

static u32_t write_buffer[160*1024];
static u32_t read_buffer[160*1024];

xdisk_t disk;
xdisk_part_t disk_part;
xfat_t xfat;

// io测试，测试通过要注意关掉
int disk_io_test (void);
int disk_part_test(void);

int main (void)
{
    xfat_err_t err;
    int i;

    for (i = 0; i < sizeof(write_buffer) / sizeof(u32_t); i++) 
    {
        write_buffer[i] = i;
    }

    err = xdisk_open(&disk, "vidsk", &vdisk_driver, (void *)disk_path);
    if (err) 
    {
        printf("open disk failed!\n");
        return -1;
    }

    err = disk_part_test();
    if (err)
    {
        return err;
    }

    err = xdisk_get_part(&disk,&disk_part,1);
    if(err < 0)
    {
        printf("read partition info failed!\n");
        return -1;
    }

    
    err = xfat_open(&xfat, &disk_part);
    if (err < 0) 
    {
        return err;
    }

    err = xdisk_close(&disk);
    if (err) 
    {
        printf("disk close failed!\n");
        return -1;
    }

    printf("Test End!\n");
    return 0;
}

// io测试，测试通过要注意关掉
int disk_io_test (void)
{
    int err;
    xdisk_t disk_test;

    memset(read_buffer, 0, sizeof(read_buffer));

    err = xdisk_open(&disk_test, "vidsk_test", &vdisk_driver, (void *)disk_path_test);
    if (err) 
    {
        printf("open disk failed!\n");
        return -1;
    }

    err = xdisk_write_sector(&disk_test, (u8_t *)write_buffer, 0, 2);
    if (err) 
    {
        printf("disk write failed!\n");
        return -1;
    }

    err = xdisk_read_sector(&disk_test, (u8_t *)read_buffer, 0, 2);
    if (err) 
    {
        printf("disk read failed!\n");
        return -1;
    }

    err = memcmp((u8_t *)read_buffer, (u8_t *)write_buffer, disk_test.sector_size * 2);
    if (err != 0) 
    {
        printf("data no equal!\n");
        return -1;
    }

    err = xdisk_close(&disk_test);
    if (err) 
    {
        printf("disk close failed!\n");
        return -1;
    }

    printf("disk io test ok!\n");
    return 0;
}

int disk_part_test(void)
{
    u32_t count,i;
    xfat_err_t err = FS_ERR_OK;

    printf("partiton read test...\n");

    err = xdisk_get_part_count(&disk,&count);
    if(err < 0)
    {
        printf("partion count detect failed!\n");
        return err;
    }
    printf("partition count:%d\n",count);

	for (i = 0; i < count; i++) 
    {
		xdisk_part_t part;
		int err;

		err = xdisk_get_part(&disk, &part, i);
		if (err == -1 ) {
			printf("read partion in failed:%d\n", i);
			return -1;
		}

        printf("no.%02d: start: %d, count: %d, capacity:%.0f M\n",
               i, part.start_sector, part.total_sector,part.total_sector * disk.sector_size / 1024 / 1024.0);
    }

    return 0;
}

